﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Xml.Linq;
using Baitap3.DS_PagingDataTableAdapters;
using System.Data;
using System.Web.Services.Description;

namespace Baitap3
{
    public partial class Departments : System.Web.UI.Page
    {
        public DS_PagingData ds = new DS_PagingData();

        protected void Page_Load(object sender, EventArgs e)
        {
            DepartmentsTableAdapter d = new DepartmentsTableAdapter();
            EmployeesTableAdapter em = new EmployeesTableAdapter();
            d.Fill(ds.Departments);
            em.Fill(ds.Employees);
        }


        public bool Enabled { get; set; }

        public int ToI { get; set; }

        public int ToInt32 { get; set; }

        #region Delete
        protected void bt_delete_Click(object sender, ImageClickEventArgs e)
        {
            DepartmentsTableAdapter d = new DepartmentsTableAdapter();
            EmployeesTableAdapter em = new EmployeesTableAdapter();
            DS_PagingData.DepartmentsRow dr = ds.Departments.FindByDepartmentID(Convert.ToInt32(ddl_name.SelectedValue));
            foreach (DS_PagingData.EmployeesRow er in ds.Employees.Rows)
            {
                if (er.DepartmentID == int.Parse((ddl_name.SelectedValue)))
                {
                    em.Delete(er.EmployeeID);
                }
            }
            d.Delete(dr.DepartmentID);
            ddl_name.DataBind();
            DataList1.DataBind();
        }
        #endregion

        #region Show pannel to edit
        protected void bt_edit_Click(object sender, ImageClickEventArgs e)
        {
            label1.Visible = true;
            Panel1.Visible = true;
            DS_PagingData.DepartmentsRow dr = ds.Departments.FindByDepartmentID(Convert.ToInt32(ddl_name.SelectedValue));
            //Trước khi edit
            txt_Name.Text = dr.Name;
            txt_HomePage.Text = dr.URLHomePage;
            txt_Photo.Text = dr.URLPhoto;
        }
        #endregion

        #region Update data
        protected void bt_Submit_Click1(object sender, ImageClickEventArgs e)
        {
            DepartmentsTableAdapter d = new DepartmentsTableAdapter();
            DS_PagingData.DepartmentsRow dr = ds.Departments.FindByDepartmentID(Convert.ToInt32(ddl_name.SelectedValue));
            //Sau khi edit
            if (txt_Name.Text != null && txt_Photo.Text != null && txt_HomePage.Text != null)
            {
                d.Update(txt_Name.Text, txt_Photo.Text, txt_HomePage.Text, txt_ShowControl.Text, dr.DepartmentID);
            }
            d.Update(ds.Departments);
            ds.AcceptChanges();

            Panel1.Visible = false;
            ddl_name.DataBind();
            DataList1.DataBind();
        }
        #endregion

        #region Insert
        protected void bt_Insert_Click(object sender, ImageClickEventArgs e)
        {

            DepartmentsTableAdapter d = new DepartmentsTableAdapter();
            DS_PagingData.DepartmentsRow dr = ds.Departments.NewDepartmentsRow();

            if (txt_Name.Text != null && txt_Photo.Text != null && txt_HomePage.Text != null)
            {
                d.Insert(txt_Name.Text, txt_Photo.Text, txt_HomePage.Text, txt_ShowControl.Text);
            }
            d.Update(ds.Departments);
            ds.AcceptChanges();

            Panel1.Visible = false;
            ddl_name.DataBind();
            DataList1.DataBind();
        }
        #endregion
    }
}